import { HeadConfig, defineConfig } from 'vitepress'
import * as path from 'path'
import { fileWatch, findAllFiles, findSourceDir } from './theme/utils'
import * as fs from 'fs'
import matter from 'gray-matter'
import { rewritesRouter } from './setup/constant'
import { sidebar, nav } from './setup'

const pathResolve = (dir: string): string => {
  return path.resolve(__dirname, '.', dir)
}
console.log('当前环境', process.argv[3])
// https://vitepress.dev/reference/site-config
export default defineConfig({
  title: '周星星',
  description: '周星星',
  lastUpdated: true,
  cleanUrls: true,
  head: [
    [
      'script',
      {
        src: 'https://at.alicdn.com/t/c/font_4314570_6yibn10rb67.js',
      },
    ],
    [
      'script',
      {
        src: '/loading.js',
      },
    ],
    [
      'script',
      {
        src: '/test.js',
      },
    ],
  ],
  vite: {
    resolve: {
      alias: {
        '@': pathResolve('../src'),
      },
    },
    css: {
      // 预处理器配置项
      preprocessorOptions: {
        less: {
          math: 'always',
        },
      },
    },
    // build: {
    //   rollupOptions: {
    //     external: ["document"],
    //     output: {
    //       globals: {
    //         document: "document",
    //       },
    //     },
    //   },
    // },
  },
  markdown: {
    // config: (md) => {
    //   findAllFiles("./src")
    //     .then((res: any) => {
    //       res.forEach((file: any) => {
    //         if (file.indexOf("/src/index.md") === -1) {
    //           let dataStr = fs.readFileSync(file, "utf8"); // 读取每个md文件内容
    //           const fileMatterObj = matter(dataStr, {});
    //           if (Reflect.ownKeys(fileMatterObj.data).length === 0) {
    //             console.log("dataStr", file, fileMatterObj.data);
    //             fs.writeFileSync(file, `# iiiii ${fileMatterObj.content}`);
    //           }
    //         }
    //       });
    //     })
    //     .finally(() => {});
    // },
  },
  srcDir: 'src',
  base: process.argv[3] === '--github' ? '/' : '/blog-vitepress/',
  outDir: process.argv[3] === '--github' ? 'github-dist' : 'dist',
  themeConfig: {
    // editLink: {
    //   pattern: "https://gitee.com/yeWei0319/blog-vitepress/tree/master/:path",
    //   text: "Edit this page on GitHub",
    // },
    // https://vitepress.dev/reference/default-theme-config
    nav: nav,

    sidebar: sidebar,

    // socialLinks: [
    //   { icon: "github", link: "https://github.com/vuejs/vitepress" },
    // ],
    // search: {
    //   provider: "algolia",
    //   options: {
    //     appId: "8J64VVRP8K",
    //     apiKey: "a18e2f4cc5665f6602c5631fd868adfd",
    //     indexName: "vitepress",
    //   },
    // },
    // search: {
    //   provider: 'algolia',
    //   options: {
    //     appId: 'H6SDM0I2SF',
    //     apiKey: '996425532f40c486035e7f02a285378e',
    //     indexName: 'yewei0319-gitee',
    //   },
    // },
    search: {
      provider: 'algolia',
      options: {
        appId: 'VTK0W8N1YV',
        apiKey: '931cbd330a9c59b8e8ca949720531250',
        indexName: 'qingyeweiio',
      },
    },
  },
  transformPageData(pageData) {
    pageData.frontmatter.head ??= []
    pageData.frontmatter.head.push([
      'meta',
      {
        name: 'og:title',
        content:
          pageData.frontmatter.layout === 'home'
            ? `VitePress`
            : `${pageData.title} | VitePress`,
      },
    ])
  },
  rewrites: rewritesRouter,
})
